package com.lwl.controller.mht;

import com.github.pagehelper.PageInfo;
import com.lwl.entity.*;
import com.lwl.entity.mht.TMhtRecordDetail;
import com.lwl.entity.mht.TQuestionMht;
import com.lwl.service.mht.TQuestionMhtService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * (TQuestionMht)表控制层
 *
 * @author Lei
 * @since 2022-10-23 18:44:40
 */
@Controller
@RequestMapping("tQuestionMht")
public class TQuestionMhtController {
    /**
     * 服务对象
     */
    @Autowired
    private TQuestionMhtService tQuestionMhtService;

    @Resource
    private TQuestionMhtService fileService;

    @GetMapping("queryAll")
    @ResponseBody
    public LayData queryAll(MyPage myPage, TQuestionMht tQuestionMht){
        PageInfo tQuestionMhts = tQuestionMhtService.queryAll(myPage,tQuestionMht);
        return new LayData(tQuestionMhts);
    }

    /**
     * 添加题目
     * @param tQuestionMht
     * @return
     */
    @PostMapping("registerQuestion")
    @ResponseBody
    public LayData registerQuestion(TQuestionMht tQuestionMht){
        boolean status = tQuestionMhtService.insertQuestion(tQuestionMht);
        return new LayData(status);
    }

    /**
     * 题目唯一性校验
     * @param question
     * @return
     */
    @PostMapping("queryByName")
    @ResponseBody
    public LayData queryByName(String question){
        boolean status = tQuestionMhtService.queryQuestion(question);
        return new LayData(status);
    }

    /**
     *  删除题目
     * @return
     */
    @PostMapping("deleteQuestion")
    @ResponseBody
    public LayData deleteQuestion(Integer id){
        boolean status = false;
        // 先查询题号为"id"的题目是否已经被使用过，如果使用过则不能进行删除
        List<TMhtRecordDetail> tMhtRecordDetailList = tQuestionMhtService.queryTMhtRecordDetail(id);
        if (tMhtRecordDetailList.size()==0){
            status = tQuestionMhtService.deleteQuestion(id);
        }
        return new LayData(status);
    }

    /**
     * mht做题，查找所有题目
     */
    @GetMapping("allQuestion")
    public String allQuestion(MyPage myPage, HttpServletRequest request){
        myPage.setLimit(100);
        myPage.setPage(1);
        PageInfo all = tQuestionMhtService.getAll(myPage);
        request.setAttribute("allSub",all);
        return "mht/index";
    }

    /**
     * 通过easyExcel在题目展示页面，进行批量添加题目
     * @param file
     * @return
     */
    @PostMapping(value = "/import")
    @ResponseBody
    public LayData storageService(MultipartFile file) {
        boolean status =  fileService.dealFile(file);
        return new LayData(status);
    }

    /**
     * 批量删除
     * @param dataId
     * @return
     */
    @PostMapping("deleteMoreQuestion/{dataId}")
    @ResponseBody
    public LayData deleteMoreQuestion(@PathVariable Integer[] dataId){
        List<Integer> defailed = tQuestionMhtService.deleteMoreQuestion(dataId);
        return new LayData(defailed);
    }

}
